﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:ADU_REFREGASS
	/// </summary>
	public partial class ADU_REFREGASS : BaseDAL<GSLMS.Entity.TB_ADU_REFREGASS>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public ADU_REFREGASS()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_ADU_REFREGASS model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_REFREGID", model.REFREGID),
								new OracleParameter("P_CUSTNAME", model.CUSTNAME),
								new OracleParameter("P_CUSTNO", model.CUSTNO),
								new OracleParameter("P_SALESMAN", model.SALESMAN),
								new OracleParameter("P_SALESMANID", model.SALESMANID),
								new OracleParameter("P_ASSDATE", model.ASSDATE),
								new OracleParameter("P_ASSBY", model.ASSBY)};
  
			for (int i = 0; i < 8; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_refregass_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_ADU_REFREGASS model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_REFREGID", model.REFREGID),
								new OracleParameter("P_CUSTNAME", model.CUSTNAME),
								new OracleParameter("P_CUSTNO", model.CUSTNO),
								new OracleParameter("P_SALESMAN", model.SALESMAN),
								new OracleParameter("P_SALESMANID", model.SALESMANID),
								new OracleParameter("P_ASSDATE", model.ASSDATE),
								new OracleParameter("P_ASSBY", model.ASSBY)};
  
			for (int i = 0; i < 8; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_refregass_update", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
				OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_adu_refregass_delete", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_ADU_REFREGASS DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_ADU_REFREGASS model = new GSLMS.Entity.TB_ADU_REFREGASS();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["REFREGID"]!=null && dr["REFREGID"].ToString()!="")
				{
					model.REFREGID=OracleHelper.ToLong(dr["REFREGID"].ToString());
				}
				if(dr["CUSTNAME"]!=null && dr["CUSTNAME"].ToString()!="")
				{
					model.CUSTNAME=dr["CUSTNAME"].ToString();
				}
				if(dr["CUSTNO"]!=null && dr["CUSTNO"].ToString()!="")
				{
					model.CUSTNO=dr["CUSTNO"].ToString();
				}
				if(dr["SALESMAN"]!=null && dr["SALESMAN"].ToString()!="")
				{
					model.SALESMAN=dr["SALESMAN"].ToString();
				}
				if(dr["SALESMANID"]!=null && dr["SALESMANID"].ToString()!="")
				{
					model.SALESMANID=OracleHelper.ToLong(dr["SALESMANID"].ToString());
				}
				if(dr["ASSDATE"]!=null && dr["ASSDATE"].ToString()!="")
				{
					model.ASSDATE=DateTime.Parse(dr["ASSDATE"].ToString());
				}
				if(dr["ASSBY"]!=null && dr["ASSBY"].ToString()!="")
				{
					model.ASSBY=dr["ASSBY"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_ADU_REFREGASS GetByID(long ID)
		{
			GSLMS.Entity.TB_ADU_REFREGASS sModel = new GSLMS.Entity.TB_ADU_REFREGASS();
			string sql = "";
			sql = "select * from TB_ADU_REFREGASS where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_ADU_REFREGASS> GetList(GSLMS.Entity.TB_ADU_REFREGASS searchParam)
		{
			var list=new List<GSLMS.Entity.TB_ADU_REFREGASS>();
			DataTable dt = null;
			string tblName = "TB_ADU_REFREGASS";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;

            //是否计算总数
            countFlag = (searchParam.Pagination.ReCount != null && searchParam.Pagination.ReCount) ? 1 : 0;
            pageSize = (searchParam.Pagination.PageSize != null && searchParam.Pagination.PageSize > 0) ? searchParam.Pagination.PageSize : 15;
            pageIndex = (searchParam.Pagination.CurrentIndex != null && searchParam.Pagination.CurrentIndex > 0) ? searchParam.Pagination.CurrentIndex : 1;

            if (searchParam.CUSTNAME != null && searchParam.CUSTNAME.Trim() != "")
            {
                sqlWhere += " and custname like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.CUSTNAME) + "%'";
            }

            if (searchParam.CUSTNO != null && searchParam.CUSTNO.Trim() != "")
            {
                sqlWhere += " and CUSTNO like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.CUSTNO) + "%'";
            }

            if (searchParam.SALESMANID>0)
            {
                sqlWhere += " and SALESMANID=" + searchParam.SALESMANID.ToString();
            }

            if (searchParam.ASSDATE != null && searchParam.ASSDATE.ToString().Trim() != "")
            {
                sqlWhere += " and ASSDATE>=to_date('" + Convert.ToDateTime(searchParam.ASSDATE).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }

            if (searchParam.ASSDATE_END != null && searchParam.ASSDATE_END.ToString().Trim() != "")
            {
                sqlWhere += " and ASSDATE<to_date('" + Convert.ToDateTime(searchParam.ASSDATE_END).AddDays(1).AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
            }


			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
            if (searchParam.Pagination.ReCount)
            {
                searchParam.RowCount = totalCount;
                searchParam.Pagination.RowCount = totalCount;
            }
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_ADU_REFREGASS model = new GSLMS.Entity.TB_ADU_REFREGASS();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

